package org.harsh.transmission.common.codec;

import cn.hutool.core.codec.Base64;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.CorruptedFrameException;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.util.Arrays;
import org.harsh.common.Constants;
import org.harsh.common.Util;
import org.harsh.transmission.common.CustomFile;
import org.harsh.transmission.common.CustomKey;
import org.harsh.transmission.common.CustomMsg;

import java.util.List;

/**
 * Created by liuhaibo on 2024/6/12.
 */
@Slf4j
public class MsgPackDecoder extends ByteToMessageDecoder {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
//        log.info("MsgPackDecoder decode....");
        byte type = in.readByte();
        if(type == Constants.MessageType.REQUPDATEKEY.getCode() || type == Constants.MessageType.ACKUPDATEKEY.getCode()) {
            long msgId = in.readLong();
            byte[] msgData = new byte[in.readableBytes()];
            in.readBytes(msgData);
            CustomKey customKey = new CustomKey();
            customKey.parse(type, msgId, msgData);
            out.add(customKey);
        }
        else if(type == Constants.MessageType.TRANSMSG.getCode())
        {
            byte[] msgData = new byte[in.readableBytes()];
            in.readBytes(msgData);
            CustomMsg msg = new CustomMsg();
            msg.parseTransMsg(type, msgData);
            out.add(msg);
        } else if(type == Constants.MessageType.ACKTRANSMSG.getCode()) {
            byte[] msgData = new byte[in.readableBytes()];
            in.readBytes(msgData);
            CustomMsg msg = new CustomMsg();
            msg.parseTransAckMsg(type, msgData);
            out.add(msg);
        } else if(type == Constants.MessageType.TRANSFILEHEAD.getCode()) {
//            long msgId = in.readLong();
            byte[] msgData = new byte[in.readableBytes()];
            in.readBytes(msgData);
            CustomFile customFile = new CustomFile(type, msgData);
            out.add(customFile);
        } else if(type == Constants.MessageType.TRANSFILEDATA.getCode()) {
            long msgId = in.readLong();
            byte[] msgData = new byte[in.readableBytes()];
            byte[] tmp1 = new byte[1];
            tmp1[0] = type;
            in.readBytes(msgData);
//            byte[] tempData =Arrays.concatenate(tmp1, Arrays.concatenate(Util.long2Bytes(msgId), msgData));
//            log.info("Recv File Str: {}", Base64.encode(tempData));
            CustomFile customFile = new CustomFile(type, msgId);
            customFile.parseFileData(msgData);
            out.add(customFile);
        }  else if(type == Constants.MessageType.ACKTRANSFILE.getCode()) {
            // 响应信息
            long msgId = in.readLong();
            CustomFile customFile = new CustomFile(type, msgId, in.readByte(), in.readLong(), in.readLong());
            out.add(customFile);
        } else if(type == Constants.MessageType.TRANSFILEOVER.getCode() || type == Constants.MessageType.ACKTRANSFILEOVER.getCode()) {
            long msgId = in.readLong();
            byte[] msgData = new byte[in.readableBytes()];
            CustomFile customFile = new CustomFile(type, msgId);
            out.add(customFile);
        } else {
            log.error("错误的数据类型：{}", type);
        }
    }
}
